<template>
  <div>
    <el-dialog title="编辑" :visible.sync="is.show" :before-close="cancel">
      <el-form :model="memberForm" :rules="rules" ref="memberForm">
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="phone"  :label-width="formLabelWidth">
          <el-input
            v-model="memberForm.phone"
            disabled
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- nickname 昵称 -->
        <el-form-item
          label="昵称"
          prop="nickname"
          :label-width="formLabelWidth"
        >
          <el-input v-model="memberForm.nickname" clearable autocomplete="off"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="memberForm.password" type="password" show-password clearable autocomplete="off"></el-input>
          留空不修改
        </el-form-item>

        <!-- 状态 -->
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="memberForm.status"
            active-color="#13ce66"
            inactive-color="#ccc"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="enter">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>


<script>
//引入辅助性函数
import { mapState, mapActions } from "vuex";
//引入获取数据的接口 编辑的接口
import { memberInfo,memberEdit } from "../../request/api";
export default {
  data() {
    return {
      memberForm: {
        nickname: "",
        phone: "",
        password: "",
        status: 1,
      },
      formLabelWidth: "120px",
      //验证规则
      rules: {
        nickname: { required: true, message: "请输入昵称", trigger: "blur" },
        phone: { required: true, message: "请输入手机号", trigger: "blur" },
      },
    };
  },

  //计算属性 使用mapState
  computed: {
    ...mapState("member", ["is"]),
  },

  //方法 使用mapAction
  methods: {
    ...mapActions("member", ["getMember"]),

    //取消事件
    cancel() {
      //关闭弹窗
      this.$store.commit("member/changeDialog", false);
      //清空表数据
      this.memberForm = {
        nickname: "",
        phone: "",
        password: "",
        status: 1,
      };
      //清空表单验证规则
      this.$refs.memberForm.resetFields();
    },

    //点击确认按钮
    enter() {
      this.$refs.memberForm.validate((val) => {
        if (val) {
            //修改的接口
            memberEdit(this.memberForm).then(res=>{
                if(res.code==200){
                    //成功提示
                    this.$message.success(res.msg);
                    //关闭弹窗
                    this.cancel();
                    //重新获取数据
                    this.getMember();
                }else{
                    //失败提示
                    this.$message.error(res.msg);
                }
            })
        }
      });
    },

    //数据回显方法
    dataForm(uid) {
      memberInfo({ uid }).then((res) => {
        this.memberForm = res.list;
        this.memberForm.password = "";
      });
    },
  },
};
</script>


<style scoped>
</style>